﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Mailer.Security
{
	public class SecurityUtilities
	{
		/// <summary>
		/// Returns a PHP-Style SHA1-Hash of input string
		/// </summary>
		/// <param name="input">Input string</param>
		/// <returns>SHA1 Hash</returns>		
		public static string SHA1(string input)
		{
			SHA1 md5 = SHA1CryptoServiceProvider.Create();
			byte[] dataMd5 = md5.ComputeHash(Encoding.Default.GetBytes(input));
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < dataMd5.Length; i++)
				sb.AppendFormat("{0:x2}", dataMd5[i]);
			return sb.ToString();
		}

		/// <summary>
		/// Returns PHP-Style MD5-Hash of input string
		/// </summary>
		/// <param name="input">Input String</param>
		/// <returns>MD5 Hash</returns>
		public static string MD5(string input)
		{
			MD5 md5 = MD5CryptoServiceProvider.Create();
			byte[] dataMd5 = md5.ComputeHash(Encoding.Default.GetBytes(input));
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < dataMd5.Length; i++)
				sb.AppendFormat("{0:x2}", dataMd5[i]);
			return sb.ToString();
		}


		public static string HashPassword(string password, string username)
		{
			return SHA1(password);// + ":" + username);
		}
	}
}
